/*
 * Copyright (C) 2016 alchemystar, Inc. All Rights Reserved.
 */
package com.eblly;

import com.eblly.meta.MetaData;
import com.eblly.util.CompareUtil;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.mysql.jdbc.StringUtils;

/**
 * @Author eblly
 */
public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Main.sync();
    }

    /**
     */
    public static void sync() {
        Prop prop = PropKit.use("./config.properties");

        String sourceHost = prop.get("sourceHost");
        if (StringUtils.isEmptyOrWhitespaceOnly(sourceHost)) {
            System.out.println("sourceHost is miss");
            System.exit(-1);
        }
        String sourceUser = prop.get("sourceUser");
        if (StringUtils.isEmptyOrWhitespaceOnly(sourceUser)) {
            System.out.println("sourceUser is miss");
            System.exit(-1);
        }
        String sourcePass = prop.get("sourcePass");
        if (StringUtils.isEmptyOrWhitespaceOnly(sourcePass)) {
            System.out.println("sourcePass is miss");
            System.exit(-1);
        }
        String sourceSchema = prop.get("sourceSchema");
        if (StringUtils.isEmptyOrWhitespaceOnly(sourceSchema)) {
            System.out.println("sourceSchema is miss");
            System.exit(-1);
        }
        String sourceCharset = prop.get("sourceCharset");
        if (StringUtils.isEmptyOrWhitespaceOnly(sourceCharset)) {
            System.out.println("sourceCharset is miss");
            System.exit(-1);
        }
        String targetHost = prop.get("targetHost");
        if (StringUtils.isEmptyOrWhitespaceOnly(targetHost)) {
            System.out.println("targetHost is miss");
            System.exit(-1);
        }
        String targetUser = prop.get("targetUser");
        if (StringUtils.isEmptyOrWhitespaceOnly(targetUser)) {
            System.out.println("targetUser is miss");
            System.exit(-1);
        }
        String targetPass = prop.get("targetPass");
        if (StringUtils.isEmptyOrWhitespaceOnly(targetPass)) {
            System.out.println("targetPass is miss");
            System.exit(-1);
        }
        String targetSchema = prop.get("targetSchema");
        if (StringUtils.isEmptyOrWhitespaceOnly(targetSchema)) {
            System.out.println("targetSchema is miss");
            System.exit(-1);
        }
        String targetCharset = prop.get("targetCharset");
        if (StringUtils.isEmptyOrWhitespaceOnly(targetCharset)) {
            System.out.println("targetCharset is miss");
            System.exit(-1);
        }

        if (!sourceCharset.equals(targetCharset)) {
            System.out.println("source target charset not equal");
            System.out.println(-1);
        }

        MetaData source = new MetaData();
        source.setJdbcUrl("jdbc:mysql://" + sourceHost + "/" + sourceSchema + "?characterEncoding=" + sourceCharset);
        source.setUser(sourceUser);
        source.setPassword(sourcePass);
        source.setSchema(sourceSchema);
        source.init();

        System.out.println("=====================================================================");

        MetaData target = new MetaData();
        target.setJdbcUrl("jdbc:mysql://" + targetHost + "/" + targetSchema + "?characterEncoding=" + targetCharset);
        target.setUser(targetUser);
        target.setPassword(targetPass);
        target.setSchema(targetSchema);
        target.init();

        CompareUtil units = new CompareUtil(source, target);
        units.compare();

        System.out.println();
        System.out.println("SET FOREIGN_KEY_CHECKS = 0; ");

        while (units.getChangeSqlDeque().size() > 0) {
            System.out.println(units.getChangeSqlDeque().pollFirst());
        }

        System.out.println("SET FOREIGN_KEY_CHECKS = 1; ");
        System.out.println();
    }
}

